package com.genITeam.ria.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.genITeam.ria.bl.EventBL;
import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.utility.Utility;
import com.genITeam.ria.vo.Events;
import com.genITeam.ria.vo.Member;


public class EventAction extends AbstractAction{

	Logger logger = Logger.getLogger(EventAction.class);


	public void handleEvent(HttpServletRequest request, HttpServletResponse response) throws NGFException{
		
		logger.info("start handleEvent");				

		EventBL eventBL = new EventBL();
		
		try{
		
			String actionType = request.getParameter("type");
			
			if(actionType.equalsIgnoreCase("View")){
				System.out.print("View Type");
				this.initAction(request, response);
				this.writeResponse(eventBL.getEventDetail());
				
			}else if(actionType.equalsIgnoreCase("Add")){
				
				Member memberSender = new Member();
				Events events = new Events();
				
				String senderId = request.getParameter("senderId");
				String city = request.getParameter("city");
				String detail = request.getParameter("detail");
				String eventDate = request.getParameter("eventDate");		
				System.out.println("eventDate=  "+ eventDate);
				String eventTime = request.getParameter("eventTime");
				System.out.println("eventTime=  "+ eventTime);
				String location = request.getParameter("location");
				String title = request.getParameter("title");
				
				memberSender.setId(Integer.parseInt(senderId));
				events.setMember(memberSender);
				events.setCity(city);
				events.setDetail(detail);
				events.setEventDate(Utility.convertStringToDate(eventDate, "dd-MM-yyyy"));
				events.setEventTime(Utility.convertStringToTime(eventTime, "hh:mm:ss"));
				events.setLocation(location);
				events.setTitle(title);
				
				System.out.println("eventDate=  "+ events.getEventDate());
				System.out.println("eventTime=  "+ events.getEventTime());
				
				eventBL.saveEvent(events);	
				this.initAction(request, response);				
				this.writeResponse("<message>Message Sent</message>");
				
			}else if(actionType.equalsIgnoreCase("Delete")){
				
				String eventId= request.getParameter("eventId");		
				Events events = new Events();
				events.setId(Integer.parseInt(eventId));
				
				eventBL.deleteEvent(events);
				
				this.initAction(request, response);				
				this.writeResponse("<message>Message Deleted</message>");				
			}


		} catch (NGFException e) {
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getErrorMessage() + "</error>");
			System.out.print("Search Forum Excepton = "+ e.getMessage());
		} catch (Exception e) {
			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");
			System.out.print("Search Forum Excepton = "+ e.getMessage());
		}    
				
	}
}
